var express = require('express');
var router = express.Router();

const sql = require("../sql/sql");
const collect = require("../sql/collect");
const pro = require("../sql/pro");
const tools = require("../tools/index");
const uuid = require("node-uuid");
/**
 * @apiGroup 收藏
 * @api {post} /api/collect/add 添加收藏
 * @apiVersion 0.0.0
 * @apiDescription 根据 userId 和 proId 添加收藏数据
 * @apiParam {String} userId 接收用户登录之后的id
 * @apiParam {String} proId 接收要收藏的proId
 * @apiSuccess {number} state 请求状态
 * @apiSuccess {string} title 请求说明
 * @apiSuccessExample {json} Success-Response:
 * {
 *    "state":200,
 *    "title":"添加成功"
 * }
 * @apiSampleRequest http://localhost:3000/api/collect/add
 */

// 添加收藏
router.post('/add', (req, res, next) => {
    let { userId, proId } = req.body;
    // let token = req.headers.token;
    // tools.verify(token).then(response => {
    sql.insert(collect, {
            userId,
            proId,
            time: Date.now()
        }).then(() => {
            res.send({
                state: 200,
                title: "添加成功"
            })
        })
        // }).catch(err => {
        //     res.send({
        //         state: 10086,
        //         title: "登录失效"
        //     })
        // })
})

/**
 * @apiGroup 收藏
 * @api {post} /api/collect/delete 取消收藏
 * @apiVersion 0.0.0
 * @apiDescription 根据 userId 和 proId 取消收藏数据
 * @apiParam {String} userId 接收用户登录之后的id
 * @apiParam {String} proId 接收要取消收藏的proId
 * @apiSuccess {number} state 请求状态
 * @apiSuccess {string} title 请求说明
 * @apiSuccessExample {json} Success-Response:
 * {
 *    "state":200,
 *    "title":"删除成功"
 * }
 * @apiSampleRequest http://localhost:3000/api/collect/delete
 */

// 取消收藏
router.post('/delete', (req, res, next) => {
    let { userId, proId } = req.body;
    let token = req.headers.token;
    // tools.verify(token).then(response => {
    sql.delete(collect, {
            userId,
            proId
        }).then(() => {
            res.send({
                state: 200,
                title: "删除成功"
            })
        })
        // }).catch(err => {
        //     res.send({
        //         state: 10086,
        //         title: "登录失效"
        //     })
        // })
})

/**
 * @apiGroup 收藏
 * @api {post} /api/collect/get 判断是否收藏
 * @apiVersion 0.0.0
 * @apiDescription 根据 userId 和proId 获取收藏数据
 * @apiParam {String} userId 接收用户登录之后的id
 * @apiParam {String} proId 接收商品的id
 * @apiSuccess {number} state 请求状态
 * @apiSuccess {string} title 请求说明
 * @apiSuccessExample {json} Success-Response:
 * {
 *    "state":200,
 *    "title":"获取成功"
 * }
 * @apiSampleRequest http://localhost:3000/api/collect/get
 */

// 获取收藏
router.post('/get', (req, res, next) => {
    let { userId, proId } = req.body;
    let token = req.headers.token;
    // tools.verify(token).then(response => {
    sql.find(collect, { userId, proId }).then(data => {
            res.send({
                state: 200,
                title: "获取成功",
                data
            })
        })
        // }).catch(err => {
        //     res.send({
        //         state: 10086,
        //         title: "登录失效"
        //     })
        // })
})

/**
 * @apiGroup 收藏
 * @api {post} /api/collect/getAll 获取用户所有
 * @apiVersion 0.0.0
 * @apiDescription 根据 userId 获取用户所有收藏数据
 * @apiParam {String} userId 接收用户登录之后的id
 * @apiSuccess {number} state 请求状态
 * @apiSuccess {string} title 请求说明
 * @apiSuccessExample {json} Success-Response:
 * {
 *    "state":200,
 *    "title":"获取成功"
 * }
 * @apiSampleRequest http://localhost:3000/api/collect/getAll
 */

// 获取用户所有收藏
router.post('/getAll', (req, res, next) => {
    let { userId } = req.body;
    let token = req.headers.token;
    // tools.verify(token).then(response => {
    sql.find(collect, { userId }).then(data => {
            res.send({
                state: 200,
                title: "获取成功",
                data
            })
        })
        // }).catch(err => {
        //     res.send({
        //         state: 10086,
        //         title: "登录失效"
        //     })
        // })
})

module.exports = router;